package ui.forms;

import base.BackgroundTask;
import base.Category;
import base.Food;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import db.CategoryManager;
import db.FoodManager;
import java.rmi.RemoteException;
import java.util.Vector;
import stub.CategoryServicesSoap_Stub;

/**
 * Defines the layout for the form.Displays the food in the database in a list.
 * User can add selected food into local database.Displays a status message on completion.
 *
 * @author Meera, Shayan
 */
public class AddFoodForm extends Form implements ActionListener {

    private int formHeight;
    private List lstPrdt;
    private Vector foodsVector;
    ListCellRenderer listrenderer;
    private TextField txtName = new TextField("", 20);
    FilterProxyListModel proxyModel;
    private Label lblName = new Label("Name:");
    public Command cmdAdd;
    public Context parent;

    /**
     * Defines the layout for the form.Displays the food in the database in a list.
     * User can add selected food into local database.Displays a status message on completion.
     */
    public AddFoodForm(final Form parent) {
        setLayout(new BorderLayout());

        formHeight = getPreferredH();
        cmdAdd = new Command("Add");
        Button btnAdd = new Button(cmdAdd);

        foodsVector = new Vector(20);
        proxyModel = new FilterProxyListModel(new DefaultListModel(foodsVector));

        lstPrdt = new List(proxyModel);
        lstPrdt.setSmoothScrolling(true);
        lstPrdt.getStyle().setBorder(null);
        lstPrdt.setPreferredH(formHeight / 2);

        listrenderer = new AddFoodListRenderer();
        lstPrdt.setListCellRenderer(listrenderer);

        Container conAdd = new Container(new FlowLayout(Component.CENTER));
        conAdd.addComponent(btnAdd);

        Container cont1 = new Container(new BoxLayout(BoxLayout.X_AXIS));

        if ((lblName.getParent() != null) && (txtName.getParent() != null)) {
            lblName.getParent().removeComponent(lblName);
            txtName.getParent().removeComponent(txtName);
        }

        if ((lblName.getParent() == null) && (txtName.getParent() == null)) {
            cont1.addComponent(lblName);
            cont1.addComponent(txtName);
        }
        addComponent(BorderLayout.NORTH, cont1);
        addComponent(BorderLayout.CENTER, lstPrdt);
        addComponent(BorderLayout.SOUTH, conAdd);
        addCommandListener(this);
        txtName.addDataChangeListener(new DataChangedListener() {

            public void dataChanged(int type, int index) {
                new BackgroundTask() {

                    public void performTask() {
                        try {
                            proxyModel.filter(txtName.getText());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            Dialog.show("Error", "Error:" + ex.getMessage(), "OK", "");
                        }
                    }
                }.start();
            }
        });

        addCommand(new Command("Back") {

            public void actionPerformed(ActionEvent evt) {
                parent.show();
            }
        });

        addCommand(new Command("Change Server") {

            public void actionPerformed(ActionEvent evt) {
                ChangeServerForm form = new ChangeServerForm(AddFoodForm.this);
                form.show();
            }
        });

    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getCommand() == cmdAdd) {
            int index = proxyModel.getSelectedIndex();
            Food food = (Food) proxyModel.getItemAt(index);
            try {
                if (FoodManager.getById(food.getId()) == null) {
                    FoodManager.persist(food);

                    StringBuffer sb = new StringBuffer("Food added successfully.");
                    sb.append("\nName:");
                    sb.append(food.getName());
                    sb.append("\nCalories:");
                    sb.append(food.getCalories());
                    Dialog.show("Food Added", sb.toString(), "OK", "");
                } else {
                    Dialog.show("Error", "This food already exists", "OK", "");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                Dialog.show("Error", ex.getMessage(), "OK", "");
            }
        }

    }
}
